package com.xinhua.geng.controller;


import com.xinhua.geng.pojo.*;
import com.xinhua.geng.service.UserService;
import com.xinhua.geng.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping("/name")
    public List<Object> getName(){
        if(redisUtil.lGet("name",0,-1).size() == 0){
            return userService.selectName();
        }
        return redisUtil.lGet("name",0,-1);
    }

    @RequestMapping(value = "/findUser/{name}/{password}",produces = "application/json;charset=utf-8")
    public Map getUser(@PathVariable String name, @PathVariable String password){

            return  userService.fingUser(name,password);
    }

    @RequestMapping(value = "/findClass/{classNo}",produces = "application/json;charset=utf-8")
    public List<? extends Object> getClass(@PathVariable String classNo){
        List<Object> users = redisUtil.lGet("find_class"+classNo,0,-1);
        if(users == null ||users.size() == 0) {
            return userService.findClass(classNo);
        }
        return users;
    }

    @RequestMapping(value = "/getUser/{id}",produces = "application/json;charset=utf-8")
    public User getUser(@PathVariable int id){

        User user = (User) redisUtil.get("get_user"+id);
        if(user == null){
            return userService.getUser(id);
        }
        return user;
    }

    @RequestMapping(value = "/classCount",produces = "application/json;charset=utf-8")
    public List<? extends Object> classCount(){
        List<Object> totalOnes = redisUtil.lGet("class_count",0,-1);
        if(totalOnes == null || totalOnes.size() == 0){
            return userService.classCount();
        }
        return totalOnes;
    }

    @RequestMapping(value = "/ageCount",produces = "application/json;charset=utf-8")
    public List<? extends Object> ageCount(){
        List<Object> totalTwos = redisUtil.lGet("age_count",0,-1);
        if(totalTwos == null || totalTwos.size() == 0){
            return userService.ageCount();
        }
        return totalTwos;
    }

    @RequestMapping(value = "/getUserByName/{name}",produces = "application/json;charset=utf-8")
    public List<? extends Object> getUserByName(@PathVariable String name){

        List<Object> users = redisUtil.lGet("findu_name"+name,0,-1);
        if(users == null || users.size() == 0){
            return userService.findUserByName(name);
        }
        return users;
    }

    @RequestMapping(value = "/getKao",produces = "application/json;charset=utf-8")
    public List<? extends Object> getKao(){
        List<Object> kaos = redisUtil.lGet("kao",0,-1);
        if(kaos == null || kaos.size() == 0){
            return  userService.getKao();
        }
        return kaos;
    }

    @RequestMapping(value = "/getScore/{id}",produces = "application/json;charset=utf-8")
    public List<? extends Object> getScore(@PathVariable int id){
        List<Object> list = redisUtil.lGet("get_score"+id,0,-1);
        if(list == null || list.size() == 0 ){
            return  userService.getScore(id);
        }
        return list;
    }

    @RequestMapping(value = "/deleteUser/{id}",produces = "application/json;charset=utf-8")
    public boolean deleteUser(@PathVariable int id){


        if(userService.deleteStu(id)){
            return true;
        }else {
            return false;
        }

    }

    @RequestMapping(value = "/setUser/{id}/{name}/{age}/{sex}/{classNo}/{ji}",produces = "application/json;charset=utf-8")
    public boolean setUser(@PathVariable int id, @PathVariable String name, @PathVariable int age, @PathVariable String sex, @PathVariable String classNo, @PathVariable String ji){

        boolean b = userService.setUser(id, name, age, sex, classNo, ji);

        return b;
    }

    @RequestMapping(value = "/addUser/{name}/{age}/{sex}/{classNo}/{ji}",produces = "application/json;charset=utf-8")

    public boolean addUser(@PathVariable String name, @PathVariable int age, @PathVariable String sex, @PathVariable String classNo, @PathVariable String ji){
        boolean a = userService.addUser(name, age, sex, classNo, ji);
        int id = userService.selectMax();
        System.out.println(id);
        boolean b = userService.insertScore(id);
        if(a && b){
            return  true;
        }else {
            return false;
        }
    }


}
